#include "ReutersDataView.hpp"
#include "ReutersDocument.hpp"

#include <utility>


#define SINGLE_CLASS_MODE


ReutersDataView::ReutersDataView() :
    setCount_ (0)
{}


void ReutersDataView::add (const QSet<ReutersDocument*>& docs) {
    if (setCount_ == 0) {
        docSet_ = docs;
    } else {
#ifndef SINGLE_CLASS_MODE
        // in multi class per document mode we want the filter that
        // shows documents belonging to all selected classes
        docSet_.intersect(docs);
#else
        // in single class per document mode we want the filter that
        // shows documents belonging to any of the selected classes
        docSet_.unite(docs);
#endif
    }
    ++setCount_;
}


void ReutersDataView::clear () {
    docSet_.clear();
    docList_.clear();
    setCount_ = 0;
}


void ReutersDataView::updateFinished () {
    docList_ = docSet_.toList();
}


int ReutersDataView::rowCount (const QModelIndex&) const {
    if (setCount_ != 0)
        return docList_.size();
    else
        return allDocs_ != nullptr ? allDocs_->size() : 0;
}


QVariant ReutersDataView::data (const QModelIndex& index, int role) const {
    if (role == Qt::DisplayRole)
        return QVariant(
                setCount_ != 0 ?
                    docList_.at(index.row())->title()
                :   allDocs_->at(index.row())->title()
            );
    else
        return QVariant ();
}
